// mircoApp/cloud_shop/brand_wall/brand_wall.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // 专题id
    subject_id: null,
    time_type: 0,
    // 排序
    sort_type: 0,
    // 上架品牌，下架品牌
    disabled_type: 1,
    // 订阅
    is_follow: 0,
    // 门店店长
    storeManager: false,
    downShow: false,
    checked: false,
    bandList: [],
    subList: [],
    follow_brand_count: 0,
    // 品牌id，下架上架用
    brand: {
      id: null,
      status: null,
    },
    brand_name: "",
    page: 1,
    total_page: 0,
    isLoadMore: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.init();
    this.subjectList();
    this.getData();
  },
  searchTap() {
    this.init();
    this.getData();
  },
  handleScroll() {
    console.log(this.data.page, this.data.total_page)
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了')
    }
  },
  tapNav(e) {
    let key = e.currentTarget.dataset.key;
    this.setData({
      navActive: key,
    });
    this.subjectList();
  },
  tap_collect_listShow(e){
    let {index} = e.currentTarget.dataset;
    this.setData({
      ['bandList['+index+'].collect_listShow'] : this.data.bandList[index].collect_listShow ? false : true
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
  downShow_close() {
    this.setData({
      downShow: false,
      "brand.id": "",
    });
  },
  downShowTap(e) {
    let {
      item
    } = e.currentTarget.dataset;
    this.setData({
      downShow: true,
      "brand.id": item.id,
      "brand.status": item.status,
    });
  },
  to_brand_home(e) {
    let {
      key
    } = e.currentTarget.dataset;
    console.log(key, "brand_home");
    wx.navigateTo({
      url: `/mircoApp/cloud_shop/brand_home/brand_home?id=${key}`,
    })
  },
  tapSort(e) {
    let {
      key
    } = e.currentTarget.dataset;
    this.setData({
      sort_type: key,
    });
    this.init();
    this.getData();
  },
  tapDsiabled(e) {
    let {
      key
    } = e.currentTarget.dataset;
    this.setData({
      disabled_type: key,
    });
    this.init();
    this.getData();
  },
  tapType(e) {
    let {
      key
    } = e.currentTarget.dataset;
    this.setData({
      time_type: key,
    });
    this.init();
    this.getData();
  },
  tapNav(e) {
    let {
      key
    } = e.currentTarget.dataset;
    if (key == "is_follow") {
      this.setData({
        is_follow: 1,
        subject_id: null,
      });
    } else {
      this.setData({
        subject_id: key == 'null' ? null : key,
        is_follow: null,
      });
    }
    this.setData({
      time_type: 0,
      disabled_type: 1,
      sort_type: 0,
    });
    this.init();
    this.getData();
  },
  storeManagerTap() {
    this.setData({
      storeManager: !this.data.storeManager,
    });
  },
  init() {
    this.setData({
      page: 1,
      total_page: 0,
      bandList: [],
      isLoadMore: true,
    });
  },
  async subjectList() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.cloud-shop.frontend.brand.subjectList"),
    });
    if (data.result) {
      this.setData({
        subList: data.data.list,
        follow_brand_count: data.data.follow_brand_count,
      });
    } else {
      app.tips(data.msg);
    }
  },
  async tapBandDown() {
    let json = {
      brand_id: this.data.brand.id,
      status: this.data.brand.status == 1 ? 0 : 1
    }
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.cloud-shop.frontend.brand.disabled"),
      data: json,
    });
    if (data.result) {
      this.init();
      this.getData();
      this.setData({
        downShow: false
      })
    } else {
      app.tips(data.msg);
    }
  },
  async getData() {
    let json = {
      disabled_type: this.data.disabled_type,
      sort_type: this.data.sort_type,
      time_type: this.data.time_type,
      brand_name: this.data.brand_name,
    };
    if (this.data.is_follow) {
      json.is_follow = this.data.is_follow;
    } else {
      if (this.data.subject_id) {
        json.subject_id = this.data.subject_id;
      }
    }
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.cloud-shop.frontend.brand.brandList"),
      data: json,
    });
    if (data.result) {
      this.setData({
        bandList: data.data.data,
        total_page: data.data.last_page,
        is_shop_owner: data.data.is_shop_owner,
      });
    } else {
      app.tips(data.msg);
    }
  },
  async getMoreData() {
    if (!this.data.isLoadMore) return;
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      })
      let json = {
        page: this.data.page,
        disabled_type: this.data.disabled_type,
        sort_type: this.data.sort_type,
        time_type: this.data.time_type,
        brand_name: this.data.brand_name
      };
      if (this.data.is_follow) {
        json.is_follow = this.data.is_follow;
      } else {
        if (this.data.subject_id) {
          json.subject_id = this.data.subject_id;
        }
      }
      let {
        data
      } = await app._postNetWork({
        url: app.getNetAddresss("plugin.cloud-shop.frontend.brand.brandList"),
        data: json,
      });
      this.data.isLoadMore = true;
      if (data.result) {
        this.setData({
          bandList: this.data.bandList.concat(data.data.data),
        })
      } else {
        app.tips(data.msg);
        this.data.page = this.data.page - 1;
        this.data.isLoadMore = false
      }
    }
  }
});